package com.swt.gap.service.impl;


import com.swt.gap.dao.SysGroupDao;
import com.swt.gap.dao.SysGroupUserDao;
import com.swt.gap.model.entity.SysGroup;
import com.swt.gap.model.entity.SysGroupUser;
import com.swt.gap.service.SysGroupService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysGroupServiceImpl implements SysGroupService {

    private final SysGroupDao sysGroupDao;
    private final SysGroupUserDao sysGroupUserDao;

    public SysGroupServiceImpl(SysGroupDao sysGroupDao, SysGroupUserDao sysGroupUserDao) {
        this.sysGroupDao = sysGroupDao;
        this.sysGroupUserDao = sysGroupUserDao;
    }

    @Override
    public Integer addGroup(SysGroup record) {
        return sysGroupDao.insertSelective(record);
    }

    @Override
    public List<SysGroup> listAllGroup() {
        return sysGroupDao.selectAll();
    }

    @Override
    public Integer updateGroup(SysGroup record) {
        return sysGroupDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public Integer deleteGroup(Integer groupId) {
        return sysGroupDao.deleteByPrimaryKey(groupId);
    }

    @Override
    public Integer addGroupUser(SysGroupUser sysGroupUser) {
        return sysGroupUserDao.insert(sysGroupUser);
    }

    @Override
    public Integer deleteGroupUser(Integer groupId, Integer userId) {
        return sysGroupUserDao.deleteSysGroupUser(groupId, userId);
    }

    @Override
    public List<Integer> listSameGroupUserByUserId(Integer userId) {
        List<Integer> retValue = new ArrayList<>();
        retValue.add(userId);
        List<Integer> groups = sysGroupUserDao.selectGroupByUserId(userId);
        if (!CollectionUtils.isEmpty(groups)) {
            retValue.addAll(sysGroupUserDao.selectUserByGroupIds(groups));
        }
        return retValue;
    }

}
